package com.naiterui.ehp.bp.bo.emr;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class QaPageBO implements Serializable {

    /**
     * 字段或域定义：<code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 5351164108869962081L;

    /**
     * 目前每页的默认记录数
     */
    public static final int DEFAULT_PAGE_SIZE = 20;

    @ApiModelProperty("问卷标题")
    private String title;
    @ApiModelProperty("问卷预览")
    private String view;
    @ApiModelProperty("问卷数据抬头")
    private LinkedHashMap<String, String> head;
    @ApiModelProperty("已填写问卷数据")
    private Long filledNumber;
    @ApiModelProperty("未填写问卷数")
    private Long notFilledNumber;
    @ApiModelProperty("问卷发送总数")
    private Long totalNumber;
    @ApiModelProperty("下载链接")
    private String exportUrl;
    @ApiModelProperty("完成百分比")
    private String completePercent;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty("统计时间")
    private Date statisTime;
    /**
     * 页码
     */
    @ApiModelProperty(value = "页码")
    @Builder.Default
    protected int pageNo = 1;

    /**
     * 每页记录数
     */
    @ApiModelProperty("每页记录数")
    protected int pageSize = DEFAULT_PAGE_SIZE;

    /**
     * 数据列表
     */
    protected List<QaRecordBO> result = Collections.emptyList();

    /**
     * 总记录数
     */
    @ApiModelProperty("记录总条数")
    @Builder.Default
    protected int totalCount = -1;

    /**
     * 返回第一页结果的时的总记录数
     * 用于流式分页
     */
    @ApiModelProperty("请求第一页时的总记录数")
    @Builder.Default
    protected int offset = 0;

    /**
     * 偏移量(根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从0开始)
     */
    private int first;

    /**
     * 总页数
     */
    @ApiModelProperty("总页数")
    @Builder.Default
    protected int totalPages = -1;

    /**
     * 是否还有下一页
     */
    @ApiModelProperty("是否还有下一页")
    @Builder.Default
    protected boolean hasNext = false;

    /**
     * 下一页页码
     */
    @ApiModelProperty("下页的页号,序号从1")
    protected int nextPage;

    /**
     * 是否已经设置排序字段
     */
    protected boolean orderSetted;

    /**
     * 是否还有上一页
     */
    @ApiModelProperty("是否还有上一页")
    @Builder.Default
    protected boolean hasPre = false;

    /**
     * 上一页的序号
     */
    @ApiModelProperty("上页的页号,序号从1开始")
    protected int prePage;

    /**
     * 正序、倒序
     */
    protected String order;

    /**
     * 排序字段
     */
    protected String orderBy;

    /**
     * 当前页实际的记录数
     */
    @ApiModelProperty("当前页实际的记录数")
    protected int numOfElements;

    /**
     * 构造函数
     */
    public QaPageBO() {
    }

    /**
     * 构造函数
     */
    @Builder
    public QaPageBO(int pageNo, int pageSize, List<QaRecordBO> result, int totalCount, int offset, String order, String orderBy,
                    String title, String view, LinkedHashMap<String, String> head,
                    Long filledNumber, Long notFilledNumber, Long totalNumber, String exportUrl,String completePercent,Date statisTime) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.result = result;
        this.totalCount = totalCount;
        this.offset = offset;// TODO:JPA need to implements stream page
        this.first = (this.pageNo - 1) * this.pageSize;
        //////////////// 计算总页数////////////////////////
        if (this.offset > 0 && this.offset > this.totalCount) {
            this.totalCount = this.offset;
        }
        int count = this.totalCount / this.pageSize;
        if (this.totalCount % this.pageSize > 0) {
            count++;
        }
        this.totalPages = count;
        //////////////// 计算总页数////////////////////////
        this.hasNext = (this.pageNo + 1) <= this.totalPages;
        this.nextPage = this.hasNext ? this.pageNo + 1 : this.pageNo;
        this.hasPre = this.pageNo != 1;
        this.prePage = this.hasPre ? this.pageNo - 1 : this.pageNo;
        this.order = order;
        this.orderBy = orderBy;
        this.orderSetted = StringUtils.isNotBlank(this.orderBy);
        this.numOfElements = this.totalCount;

        this.title = title;
        this.view = view;
        this.head = head;

        this.filledNumber = filledNumber;
        this.notFilledNumber = notFilledNumber;
        this.totalNumber = totalNumber;

        this.exportUrl = exportUrl;
        this.completePercent = completePercent;
        this.statisTime = statisTime;
    }
}
